home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OOPTUT34.ZIP / OOPINTRO.TXT < prev    next >
Text File  |  1993-06-12  |  8KB  |  172 lines

  1.              INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING.
  2.              ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4.  It is good programming practice to develop sub-routines, procedures or
  5.  functions for blocks of code which perform well-defined and frequently
  6.  repeated tasks. The procedure or function may have 'formal' parameters,
  7.  as declared, so that it can be called with different 'actual' parameters.
  8.  
  9.  It is also sensible to bundle together any associated data such as name,
  10.  address and telephone number, into a record. Each of the associated items
  11.  of data is stored in a separate 'field' of the record and can be accessed
  12.  using the syntax <Record Name>.<Field Name>,  e.g. Personnel.Name
  13.  
  14.  Even though these techniques are employed, it is still possible to use a
  15.  procedure or function with the wrong data. In many circumstances it is
  16.  therefore wise to lock together procedures and functions with the
  17.  associated data. This is object-oriented programming or object-oriented
  18.  design (OOD) of programs.
  19.  
  20.  Object-oriented programming was introduced in the early 70's in the
  21.  simulation language Simula and then developed in the Smalltalk language.
  22.  Object-oriented design of software has only recently entered the mainstream
  23.  of computing.  Turbo Pascal, version 5.5, with Object-Oriented Programming
  24.  (OOP), was introduced in 1989.
  25.  
  26.  Object-oriented programming was based on the observation that computer
  27.  programs perform actions on objects, such as records in databases, lines of
  28.  text, and graphical shapes.  Traditional software was simply a list of
  29.  actions performed on certain data in a certain sequence.  Changing the data
  30.  or the actions usually meant changing the program, sometimes significantly,
  31.  and hence disturbing tested code.  OOP offers an opportunity to change the
  32.  way the data is manipulated by extending the program rather than changing
  33.  it, so that tested code is retained.
  34.  
  35.  An 'object' type is an extension of the structured 'record' type. However,
  36.  it also contains functions and procedures, declared locally within the
  37.  object type declaration, and these are used to manipulate the data.
  38.  These functions and procedures are jointly called 'methods'. Although the
  39.  method and its parameters are declared within the object type declaration,
  40.  the code for the implementation of the method is defined outside the object,
  41.  somewhere in the program before that particular method is called.
  42.  
  43.  Since the method is declared within the object type declaration, the method
  44.  is rigidly bound to that object type. This is called Encapsulation.
  45.  
  46.  As with records, the type is first declared in the 'type' declaration part
  47.  of the program. Then specific variables of that type are declared in the
  48.  'var' declaration part of the program.
  49.  
  50.  This can be illustrated by means of a simple example, which compares the
  51.  conventional way to initialize a record with the initialization of the
  52.  same data by means of an object defined with its own method.  The two
  53.  programs are called RECINIT.PAS and OBJINIT.PAS respectively. The type
  54.  declaration for the object is as follows:
  55.  
  56.      type
  57.         Obj = object            { compared with 'Rec = record' }
  58.           i : integer;
  59.           r : real;
  60.           s : string[50];
  61.           procedure Init( int : integer; re : real; st : string);
  62.              { this last line is not in the record type declaration }
  63.         end;
  64.  
  65.  The essential difference is that the procedure 'Init' is declared after
  66.  the data fields in the object declaration.  This Init procedure has ceased
  67.  to exist as a separate entity and is now an integral part of the 'Obj'
  68.  object type, although the code for the initialization method is defined
  69.  separately and referenced by means of the 'dot' notation as below:
  70.  
  71.      procedure Obj.Init( int : integer; re : real; st : string);
  72.  
  73.        { In the record case, the procedure name would just be 'Init',
  74.          but the parameters would include the formal 'var DataRec : Rec' }
  75.  
  76.      begin
  77.        i := int;
  78.        r := re;
  79.        s := st;
  80.      end;
  81.  
  82.  The program is completed by a 'var' declaration and a main part as follows:
  83.  
  84.      var
  85.        ThisObj  : Obj;
  86.  
  87.      begin
  88.        ClrScr;
  89.        ThisObj.Init(1234, 9.876, 'This is the string entry for this object');
  90.  
  91.           { In the record case, a final actual parameter, a rec-type
  92.             variable 'ThisRec' would be included }
  93.  
  94.        writeln( 'The integer value is ', ThisObj.i);
  95.        writeln( 'The real value is ', ThisObj.r);
  96.        writeln( 'The string is " ', ThisObj.s, '"');
  97.      end.
  98.  
  99.  If more than one variable of type 'obj' is declared, each variable would
  100.  have its own region of memory reserved for the field entries, but would
  101.  share a common pointer to the procedure 'Init'.  Furthermore, it would be
  102.  impossible to call this procedure without specifically involving an object
  103.  of type 'obj'.
  104.  
  105.  The procedure is now referred to by the dot notation as 'Obj.Init', which
  106.  tells the compiler that the procedure is part of the 'Obj' object type.
  107.  When a variable, or instance, 'ThisObj', of the type 'Obj' is declared,
  108.  the same dot notation is used  -  ThisObj.Init(....);
  109.  
  110.  Another difference is the absence of the last parameter in the procedure.
  111.  The formal parameter 'var DataRec : Rec' is not replaced with a parameter
  112.  referring to the object, since the data fields are already bound to the
  113.  procedure by the object type declaration.
  114.  
  115.  In OOP parlance, a 'class' or 'object type' is an abstraction that provides
  116.  a template for objects, which are called 'instances' of that class.
  117.  Thus 'Obj' is a template (type or class) and 'ThisObj' is an instance.
  118.  
  119.  Whereas in literary work plagiarism is unacceptable, it is positively
  120.  encouraged in OOD. Objects are inherited and extended and in many cases
  121.  there is a natural hierarchy as with graphical objects:
  122.  
  123.           location(x,y)
  124.           point   (x,y,visible)
  125.           circle  (x,y,visible,r)
  126.           arc     (x,y,visible,r,start angle,end angle)
  127.  
  128.  The data fields for location can be inherited and then extended for the
  129.  point by the addition of a boolean (visible) field. A circle then inherits
  130.  these fields and adds the radius field and so on. It is the field structure
  131.  of the object type, not actual numeric values, that is inherited. Each
  132.  instance of each object type must be initialized, so that specific
  133.  numerical values are assigned to each data field.
  134.  
  135.  A descendant object type inherits type compatibility with all its ancestor
  136.  types. This relaxation of Turbo Pascals normal type compatibili`ty rules is
  137.  explained in more detail in the note COMPAT.TXT which may be consulted
  138.  later when use is made of this fact.
  139.  
  140.  Methods to move a point from (x1,y1) to (x2,y2) can be inherited by a
  141.  circle and an arc, although the methods to show and hide the different
  142.  graphical objects will be different.
  143.  
  144.  One of the major advantages of object-oriented programming is the ability
  145.  to build a hierarchy of descendant objects, with each descendant inheriting
  146.  access to all its ancestors' code and data fields. 'Inheritance' is a major
  147.  attraction of OOD, but some care has to taken when inherited methods are
  148.  not relevant to the descendant object as for example for a point 'show'
  149.  and a circle 'show':
  150.  
  151.  procedure Point.Show;
  152.  begin
  153.    Visible := True;             {boolean field is set 'true' for information}
  154.    PutPixel(X, Y, GetColor);    {pixel at X,Y is 'put' in foreground colour}
  155.  end;
  156.  
  157.  procedure Circle.Show;
  158.  begin
  159.    Visible := True;              {boolean field for information & inspection}
  160.    Graph.Circle(X, Y, Radius);   {draw circle in current foreground colour}
  161.  end;
  162.  
  163.  In this case the inherited method 'Show' from the object type point must be
  164.  overridden for the object type circle. This is achieved by using 'Virtual
  165.  Methods', which is explained in detail in the note VIRTUAL.TXT which should
  166.  be read next.
  167.  
  168.  
  169.  OOPINTRO.TXT
  170.  revised 31.5.93
  171.  
  172.